package tasker;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.mt4j.MTApplication;
import org.mt4j.components.MTComponent;
import org.mt4j.components.TransformSpace;
import org.mt4j.components.visibleComponents.font.FontManager;
import org.mt4j.components.visibleComponents.font.IFont;
import org.mt4j.components.visibleComponents.shapes.AbstractShape;
import org.mt4j.components.visibleComponents.shapes.MTRectangle.PositionAnchor;
import org.mt4j.components.visibleComponents.widgets.MTList;
import org.mt4j.components.visibleComponents.widgets.MTListCell;
import org.mt4j.components.visibleComponents.widgets.MTTextArea;
import org.mt4j.components.visibleComponents.widgets.MTTextArea.ExpandDirection;
import org.mt4j.components.visibleComponents.widgets.buttons.MTImageButton;
import org.mt4j.components.visibleComponents.widgets.buttons.MTSvgButton;
import org.mt4j.components.visibleComponents.widgets.keyboard.MTKeyboard;
import org.mt4j.input.gestureAction.DefaultDragAction;
import org.mt4j.input.gestureAction.InertiaDragAction;
import org.mt4j.input.inputProcessors.IGestureEventListener;
import org.mt4j.input.inputProcessors.MTGestureEvent;
import org.mt4j.input.inputProcessors.componentProcessors.dragProcessor.DragProcessor;
import org.mt4j.input.inputProcessors.componentProcessors.tapProcessor.TapEvent;
import org.mt4j.input.inputProcessors.componentProcessors.tapProcessor.TapProcessor;
import org.mt4j.input.inputProcessors.globalProcessors.CursorTracer;
import org.mt4j.sceneManagement.AbstractScene;
import org.mt4j.sceneManagement.IPreDrawAction;
import org.mt4j.sceneManagement.Iscene;
import org.mt4j.util.MT4jSettings;
import org.mt4j.util.MTColor;
import org.mt4j.util.math.Vector3D;


import processing.core.PImage;

public class MTGesturesScene extends AbstractScene {
	private MTApplication app;
	
	//Cria uma lista de MTTextArea 
	private List<MTTasker> listaTarefas;
	
	MTPanel painel;
	
	//private ArrayList<MTTextArea> doingList;
	private Iscene scene2;
	
	private String imagePath = "tasker" + MTApplication.separator + "data" + MTApplication.separator;

	public MTGesturesScene(MTApplication mtApplication, String name) {
		super(mtApplication, name);
		this.app = mtApplication;
		this.setClearColor(new MTColor(126, 130, 168, 255));
		this.registerGlobalInputProcessor(new CursorTracer(app, this));

		final float verticalPad = 50;
		final float horizontalPad = 10;
		
		
		MTColor white = new MTColor(255,255,255);
		final MTColor textAreaColor = new MTColor(50,50,50,255);
		
		final IFont font1 = FontManager.getInstance().createFont(app, "arial.ttf", 35, white, white);
		final IFont font2 = FontManager.getInstance().createFont(app, "arial.ttf", 35, MTColor.BLACK, MTColor.BLACK);
		
		//Lista de tarefas
		this.listaTarefas = new ArrayList<MTTasker>(); 
		
		//Load from file system - repetido - vrf depois
		PImage imgPanel1 = app.loadImage(imagePath + "Quadro_font_A_linha_A_barra.png");		
		
		this.painel = new MTPanel(imgPanel1, app);
		painel.setAnchor(PositionAnchor.UPPER_LEFT);
		this.getCanvas().addChild(painel);
		
		//Load from file system 
		PImage keyboardImg = app.loadImage(imagePath + "add_post_it.png");
		
		final MTImageButton keyboardButton1 = new MTImageButton(keyboardImg, app);
		keyboardButton1.setFillColor(new MTColor(255,255,255,200));
		keyboardButton1.setName("KeyboardButton1");
		keyboardButton1.setNoStroke(true);
		keyboardButton1.setSizeLocal(50, 50);

		keyboardButton1.translateGlobal(new Vector3D(-2,app.height-keyboardButton1.getWidthXY(TransformSpace.GLOBAL)+2,0));
		this.getCanvas().addChild(keyboardButton1);
		
		keyboardButton1.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae) {
				switch (ae.getID()) {
				case TapEvent.BUTTON_CLICKED:
					//Flickr Keyboard
			        MTKeyboard keyb1 = new MTKeyboard(app);
			        keyb1.setFillColor(new MTColor(30, 30, 30, 210));
			        keyb1.setStrokeColor(new MTColor(0,0,0,255));
			        keyb1.setSizeXYGlobal(300, 150);
			        
			        final MTTextArea t1 = new MTTextArea(app, FontManager.getInstance().createFont(app, "arial.ttf", 50, 
			        		new MTColor(0,0,0,255), //Fill color 
							new MTColor(0,0,0,255))); //Stroke color
			        
			        t1.setExpandDirection(ExpandDirection.UP);
					t1.setStrokeColor(new MTColor(0,0 , 0, 255));
					t1.setFillColor(new MTColor(205,200,177, 255));
					t1.unregisterAllInputProcessors();
					t1.setEnableCaret(true);
					t1.snapToKeyboard(keyb1);
					keyb1.addTextInputListener(t1);
			        
			        //Flickr Button for the keyboard
			        MTSvgButton flickrButton1 = new MTSvgButton( imagePath + "Flickr_Logo.svg", app);
			        flickrButton1.scale(0.4f, 0.4f, 1, new Vector3D(0,0,0), TransformSpace.LOCAL);
			        flickrButton1.translate(new Vector3D(0, 15,0));
			        flickrButton1.setBoundsPickingBehaviour(AbstractShape.BOUNDS_ONLY_CHECK);

			      //Add actionlistener to flickr button
			        flickrButton1.addActionListener(new ActionListener() {
						
			        	int p1 = 1;
			        	public void actionPerformed(ActionEvent arg0) {
							
							if (arg0.getSource() instanceof MTComponent){
								//MTBaseComponent clickedComp = (MTBaseComponent)arg0.getSource();
								switch (arg0.getID()) {
								case TapEvent.BUTTON_CLICKED:
		        
							        System.out.println("Flickr search for: \"" + t1.getText() + "\"");
					        
							        //Cria um componente MTTextArea
							        MTTasker tarefa= new MTTasker(app, font2);
							        painel.addChild(tarefa);
							        tarefa.setTipo("TO BE DONE");
							        tarefa.setFillColor(MTColor.YELLOW);
							        tarefa.setAnchor(PositionAnchor.UPPER_LEFT);
							        tarefa.setPositionRelativeToParent(painel.getPosition(TransformSpace.LOCAL));
							        //tarefa.setStrokeColor(MTColor.BLACK);
							        tarefa.setText(t1.getText());
									clearAllGestures(tarefa);
									tarefa.registerInputProcessor(new DragProcessor(app));
									tarefa.addGestureListener(DragProcessor.class, new DefaultDragAction());
									tarefa.addGestureListener(DragProcessor.class, new InertiaDragAction()); //Add inertia to dragging
									tarefa.setPositionGlobal(new Vector3D(horizontalPad, p1*verticalPad+40,0));
									//tarefa.setPositionRelativeToParent(painelToBeDone.getPosition(TransformSpace.LOCAL));
									p1++;
									
									listaTarefas.add(tarefa); 
							    							        
						        t1.clear();
									break;
								default:
									break;
								}
							}
						}
					});
					keyb1.addChild(flickrButton1);
					
					getCanvas().addChild(keyb1);
					keyb1.setPositionGlobal(new Vector3D(app.width/2f, app.height/2f,0));
					
					
					System.out.println("Flickr search for: \"" + t1.getText() + "\"");	
					break;
				//case TapEvent.:
					
				default:
					break;
				}
			}
		});
        
		//Button to get to the next scene
				PImage arrow = mtApplication.loadImage(imagePath +	"arrowRight.png");
				MTImageButton nextSceneButton = new MTImageButton(arrow, mtApplication);
				nextSceneButton.setNoStroke(true);
				if (MT4jSettings.getInstance().isOpenGlMode())
					nextSceneButton.setUseDirectGL(true);
				nextSceneButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent ae) {
						switch (ae.getID()) {
						case TapEvent.BUTTON_CLICKED:
							//Save the current scene on the scene stack before changing
							app.pushScene();
							if (scene2 == null){
								scene2 = new Scene2(app, "Scene 2");
								//Add the scene to the mt application
								app.addScene(scene2);
							}
							//Do the scene change
							app.changeScene(scene2);
							break;
						default:
							break;
						}
					}
				});
				getCanvas().addChild(nextSceneButton);
				nextSceneButton.setPositionGlobal(new Vector3D(app.width - nextSceneButton.getWidthXY(TransformSpace.GLOBAL) - 5, app.height - nextSceneButton.getHeightXY(TransformSpace.GLOBAL) - 5, 0));
			
	}
	

	private void clearAllGestures(MTComponent comp){
		comp.unregisterAllInputProcessors();
		comp.removeAllGestureEventListeners();
	}
	
	//private MTTextArea 
	
	@Override
	public void init() {

	}

	@Override
	public void shutDown() {

	}
	

	
	

}
